<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="wpmp.security.client.utils.ButtonConfigUtils"%>
<%@ include file="/commons/taglibs.jsp"%>
<html>
<head>
<title>配置管理</title>
<%@ include file="/commons/meta.jsp" %>
<%@ include file="/commons/header.jsp" %>
<%@ include file="/extjs/jsps/importExt.jsp" %>
<%@ include file="/extjs/jsps/importEasyExt.jsp" %>
<script language="javascript" type="text/javascript">
var store = new Ext.data.JsonStore({
	autoDestroy: true,
	autoLoad: false,
	url: '${ctx}/code!listJSON.action',
   	root: 'records',
	fields: [
        {name: 'id',mapping: 'id',type: 'string'},
        {name: 'text',mapping: 'text',type: 'string'},
        {name: 'value',mapping: 'value',type: 'string'},
        {name: 'updateUser',mapping: 'updateUser',type: 'string'},
        {name: 'updateUserName',mapping: 'updateUserName',type: 'string'},
        {name: 'updateTime',mapping: 'updateTime',type: 'string'}
	]
});

var sm = new Ext.grid.CheckboxSelectionModel({
	singleSelect: false
});

var cm = new Ext.grid.ColumnModel({
	defaults: {
		align: 'center',
		sortable: true
	},
	columns: [
		new Ext.grid.RowNumberer(), 
		sm,
		{header: 'text/key',dataIndex: 'text',width: 30},
		{header: 'value',dataIndex: 'value',width: 20},
		{header: '更新人',dataIndex: 'updateUser',width: 20,
			renderer: function(value,metaData,record,rowIndex,colIndex,store) {
				return record.get('updateUserName');
			}
		},
		{header: '更新时间',dataIndex: 'updateTime',width: 30}
	]
});

var sortStore = new Ext.data.JsonStore({
	autoDestroy: true,
	autoLoad: true,
	url: '${ctx}/codeSort!listJSON.action',
   	root: 'records',
	fields: [
        {name: 'id',mapping: 'id',type: 'string'},
        {name: 'name',mapping: 'name',type: 'string'}
	]
});
var sortSM = new Ext.grid.CheckboxSelectionModel({
	singleSelect: true,
	listeners: {
		rowselect: function(thiz,rowIndex,record) {
			store.setBaseParam("sortId", record.get("id"));
			store.load();
		}
	}
});

var sortCM = new Ext.grid.ColumnModel({
	defaults: {
		align: 'center',
		sortable: true
	},
	columns: [
		new Ext.grid.RowNumberer(), 
		sortSM,
		{header: '名称',dataIndex: 'name',width: 100}
	]
});
Ext.onReady(function() {
	var viewport = new Ext.Viewport({
		layout: 'border',
		items: [{
			title: '配置类别',
			region: 'west',
			width: 225,
			layout: 'fit',
			collapsible: true,
			items: [{
				xtype: 'editorgrid',
				style: 'text-align: left',
				border: true,
				stripeRows: true,
				loadMask: {
					msg: '加载数据中, 请稍侯... ...'
				},
				trackMouseOver: true,
				store: sortStore,
				autoExpandColumn: 'name',
				cm: sortCM,
				sm: sortSM,
				clicksToEdit: 1,
				viewConfig: {
					scrollOffset: 20,
					forceFit: true
				},
				tbar: [{
					text: '刷新',
					iconCls: "refreshBtn",
					handler: function() {
						sortStore.reload();
					}
				},'-',{
					text: '新建',
					iconCls: "addBtn",
					hidden: <%=ButtonConfigUtils.hidden(request, response, "codeSort.list.add")%>,
					handler: function() {
						var url="${ctx}/codeSort!input.action?winId=winInsertCodeSort&U="+(new Date().getTime());
						windowOpen(url,'winInsertCodeSort','新建配置类别',205,355,'','',{
							onClose: function(scope){
								var obj=scope.returnValue;
								if(obj){
									sortStore.reload();
								}
							}
						});
					}
				},'-',{
					text: '编辑',
					iconCls: "editBtn",
					hidden: <%=ButtonConfigUtils.hidden(request, response, "codeSort.list.edit")%>,
					handler: function() {
						if (sortSM.getCount()!=1) {
							Common.showErrorMsg('必须且仅能选择一个配置类别！');
							return;
						}
						var paraUrl="&id="+sortSM.getSelections()[0].get("id");
						var url="${ctx}/codeSort!input.action?winId=winUpdateCodeSort&U="+(new Date().getTime())+paraUrl;
						windowOpen(url,'winUpdateCodeSort','更新配置类别',205,355,'','',{
							onClose: function(scope){
								var obj=scope.returnValue;
								if(obj){
									sortStore.reload();
								}
							}
						});
					}
				},'-',{
					text: '删除',
					iconCls: "deleteBtn",
					hidden: <%=ButtonConfigUtils.hidden(request, response, "codeSort.list.delete")%>,
					handler: function() {
						if(sortSM.getCount()==0) {
							Common.showErrorMsg('请至少选择一个配置选项！');
							return false;
						}
						var records=sortSM.getSelections();
						var ids=new Array();
						for (var i=0;i<records.length;i++) {
							ids.push(records[i].get("id"));
						}
						Ext.Msg.confirm("","将同时删除配置选项，删除后数据无法恢复，确认删除？",function (e) {
							if(e=='no') {
								return;
							}
							Common.ajaxRequest('${ctx}/codeSort!deleteJSON.action',{
								ids: Common.getSelectedProperties(sortSM,'id')
							},function(success) {
								if(success) {
									sortStore.reload();
									store.setBaseParam("sortId", "-1");
									store.load();
								}
							});
						});
					}
				}]
			}]
		},{
			title: '配置选项',
			region: 'center',
			layout: 'fit',
			items: [{
				xtype: 'editorgrid',
				style: 'text-align: left',
				enableDragDrop: true,
				ddGroup: "GridDD",
				border: true,
				stripeRows: true,
				loadMask: {
					msg: '加载数据中, 请稍侯... ...'
				},
				trackMouseOver: true,
				store: store,
				autoExpandColumn: 'name',
				cm: cm,
				sm: sm,
				clicksToEdit: 1,
				viewConfig: {
					scrollOffset: 20,
					forceFit: true
				},
				tbar: [{
					text: '刷新',
					iconCls: "refreshBtn",
					handler: function() {
						store.reload();
					}
				},'-',{
					text: '新建',
					iconCls: "addBtn",
					hidden: <%=ButtonConfigUtils.hidden(request, response, "code.list.add")%>,
					handler: function() {
						if (sortSM.getCount()!=1) {
							Common.showErrorMsg('请选择配置类别！');
							return;
						}
						var paraUrl="&sortId="+sortSM.getSelections()[0].get("id");
						var url="${ctx}/code!input.action?winId=winInsertCode&U="+(new Date().getTime())+paraUrl;
						windowOpen(url,'winInsertCode','新建配置选项',255,355,'','',{
							onClose: function(scope){
								var obj=scope.returnValue;
								if(obj){
									store.reload();
								}
							}
						});
					}
				},'-',{
					text: '编辑',
					iconCls: "editBtn",
					hidden: <%=ButtonConfigUtils.hidden(request, response, "code.list.edit")%>,
					handler: function() {
						if (sm.getCount()!=1) {
							Common.showErrorMsg('必须且仅能选择一个配置选项！');
							return;
						}
						var paraUrl="&id="+sm.getSelections()[0].get("id");
						var url="${ctx}/code!input.action?winId=winUpdateCode&U="+(new Date().getTime())+paraUrl;
						windowOpen(url,'winUpdateCode','更新配置选项',255,355,'','',{
							onClose: function(scope){
								var obj=scope.returnValue;
								if(obj){
									store.reload();
								}
							}
						});
					}
				},'-',{
					text: '删除',
					iconCls: "deleteBtn",
					hidden: <%=ButtonConfigUtils.hidden(request, response, "code.list.delete")%>,
					handler: function() {
						if(sm.getCount()==0) {
							Common.showErrorMsg('请至少选择一个配置选项！');
							return false;
						}
						Ext.Msg.confirm("","删除后数据无法恢复，确认删除？",function (e) {
							if(e=='no') {
								return;
							}
							Common.ajaxRequest('${ctx}/code!deleteJSON.action',{
								sortId: sortSM.getSelections()[0].get("id"),
								ids: Common.getSelectedProperties(sm,'id')
							},function(success) {
								if(success) {
									store.reload();
								}
							});
						});
					}
				}],
				listeners: {
					afterrender: function(thiz){
					    //拖动排序必须的代码
					    new Ext.dd.DropTarget(thiz.getEl(), {
					        ddGroup: "GridDD",
					        notifyDrop: function(dd,e,data) {
					            var rows=thiz.getSelectionModel().getSelections();
					            var count=rows.length;
					            var cindex=dd.getDragData(e).rowIndex;
					            var array=[];
					            for(var i=0;i<count;i++) {
					                var index=cindex+i;
					                array.push(index);
					            }
					            store.remove(store.getById(data.selections[0].id));
					            store.insert(cindex,data.selections); 
			
					            thiz.getView().refresh(); 
					            thiz.getSelectionModel().selectRows(array); 
					            
					            var ids=new Array();
					            for(var i=0;i<store.getCount();i++) {
					            	ids.push(store.getAt(i).get('id'));
					            }
					            Common.ajaxRequest('${ctx}/code!sortJSON.action',{
					            	sortId: sortSM.getSelections()[0].get("id"),
									ids: ids
								},Ext.emptyFn,false);
					        }
					    });
					}
				}
			}]
		}]
	});
});
</script>
</head>

<body>
</body>
</html>